package com.mck.service.impl;

import com.mck.anno.OperationLog;
import com.mck.entity.CommonProblem;
import com.mck.mapper.CommonProblemMapper;
import com.mck.service.CommonProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class CommonProblemServiceImpl implements CommonProblemService {

    @Autowired
    private CommonProblemMapper commonProblemMapper;

    // 获取常见问题
    @Override
    @Cacheable(value = "commonProblems", key = "'all'", unless = "#result.isEmpty()")
    public List<CommonProblem> getCommonProblem() {
        return commonProblemMapper.getCommonProblem();
    }

    // 添加常见问题
    @OperationLog
    @Override
    @CacheEvict(value = "commonProblems", key = "'all'")
    public void addCommonProblem(CommonProblem commonProblem) {
        // 补全属性
        commonProblem.setCreateTime(LocalDate.now());
        commonProblem.setUpdateTime(LocalDate.now());

        commonProblemMapper.addCommonProblem(commonProblem);
    }

    // 修改常见问题
    @OperationLog
    @Override
    @CacheEvict(value = "commonProblems", key = "'all'")
    public void updateCommonProblem(CommonProblem commonProblem) {
        commonProblem.setUpdateTime(LocalDate.now());

        commonProblemMapper.updateCommonProblem(commonProblem);
    }

    // 删除常见问题
    @OperationLog
    @Override
    @CacheEvict(value = "commonProblems", key = "'all'")
    public void deleteCommonProblem(Integer id) {
        commonProblemMapper.deleteCommonProblem(id);
    }
}
